package com.share.example.selenium.service;

import com.share.common.utils.DateUtil;
import com.share.common.utils.DingDingUtil;
import com.share.common.utils.ThreadUtil;
import com.share.config.email.EmailUtil;
import com.share.example.selenium.model.amazon.seller.ClosePromoteForm;
import com.share.example.selenium.model.amazon.seller.PromoteForm;
import com.share.example.selenium.model.amazon.seller.PromoteTask;
import com.share.example.selenium.utils.UsDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
public abstract class AbsDriver {
    @Autowired
    protected EmailUtil emailUtil;

    @Autowired
    protected IOperateLogService operateLogService;

    @Autowired
    protected IBaseConfService baseConfService;

    @Autowired
    protected IPromoteDetailService promoteDetailService;

    public abstract void reInitDriver(WebDriver driver);

    public void updateLogById(String status, String errMsg, String otherMsg, Date updateTime, Long id, String handleId) {
        ThreadUtil.execute(() -> operateLogService.updateLogById(status, errMsg, otherMsg, updateTime, id, handleId));
    }

    public void dingdingCreatePromote(PromoteTask task, PromoteForm promotion, String errMsg) {
        List<String> msgList = new LinkedList<>();
        String taskId = task.getBenefit() + "_" + UsDateUtil.toMMDDHH(task.getStartDateTime()) + "_" + task.getPromotionSuffix();
        String promoteStartDateTime = UsDateUtil.toYYYY_MM_DD_HH_MM_SS(task.getStartDateTime());
        String commitTime = DateUtil.getDateTime();
        String token_secret[] = baseConfService.getDingDingTokenAndSecret();
        ThreadUtil.execute(() -> {
            String title = null;
            if (promotion == null) {
                title = "创建活动失败 " + taskId;
                msgList.add(title);
                msgList.add("操作时间:" + commitTime);
                msgList.add("活动开始时间:" + promoteStartDateTime);
                msgList.add("折扣率:" + task.getBenefit());
                msgList.add("asin:" + task.getAsin());
                msgList.add("商品促销名:" + task.getPromotionName());//缺少后缀
                msgList.add("错误信息:" + errMsg);
            } else {
                title = "创建活动成功 " + taskId;
                msgList.add(title);
                msgList.add("操作时间:" + commitTime);
                msgList.add("活动开始时间:" + promoteStartDateTime);
                msgList.add("折扣率:" + promotion.getBenift());
                msgList.add("asin:" + promotion.getAsin());
                msgList.add("商品促销名:" + promotion.getPromotionName());
                msgList.add("促销编号:" + promotion.getPromotionCode());
                msgList.add("优惠码:" + promotion.getCouponCode());
            }

            String msg = String.join(System.lineSeparator() + System.lineSeparator(), msgList);
            log.info("即将发送钉钉消息msg={}", msg);
            if (token_secret == null || token_secret.length != 2) {
                DingDingUtil.sendMdMsg(title, msg);
            } else {
                DingDingUtil.sendMdMsg(title, msg, token_secret[0], token_secret[1]);
            }
        });
    }

    //关闭活动钉钉消息
    public void dingdingClosePromote(ClosePromoteForm task, Boolean isSuccss, String errMsg) {
        List<String> msgList = new LinkedList<>();
        String taskId = task.getStartDateTime();
        String commitTime = DateUtil.getDateTime();
        String token_secret[] = baseConfService.getDingDingTokenAndSecret();
        ThreadUtil.execute(() -> {
            String title = null;
            if (isSuccss != null && isSuccss) {
                title = "删除活动成功 " + taskId;
            } else {
                title = "删除活动异常 " + taskId;
            }
            msgList.add(title);
            msgList.add("操作时间:" + commitTime);
            msgList.add("活动开始时间:" + task.getStartDateTime());
            msgList.add("活动跟踪码:" + task.getTrackName());
            msgList.add("活动名称:" + task.getPromotionName());
            msgList.add("活动链接:" + task.getLink());

            //失败测添加异常信息
            if (!(isSuccss != null && isSuccss)) {
                msgList.add("错误信息:" + errMsg);
            }

            String msg = String.join(System.lineSeparator() + System.lineSeparator(), msgList);
            log.info("即将发送钉钉消息msg={}", msg);
            if (token_secret == null || token_secret.length != 2) {
                DingDingUtil.sendMdMsg(title, msg);
            } else {
                DingDingUtil.sendMdMsg(title, msg, token_secret[0], token_secret[1]);
            }
        });
    }
}
